<?
    class Principio_Model_Spa extends Principio_Model_Templatable implements PrincipioLab_IDAble
    {
        use PrincipioLab_TCollection;

        private $id;
        private $mac;
        private $serial;
        private $ip;
        private $last_prov;
        private $last_firmware;
        private $prov_status;
        private $last_prov_attempt;
        private $last_prov_msg;

        private $collection_account_link;
        private $model_type_in_company;


        public function __construct(Principio_Model_Type $model_type, Principio_Model_Company_Abstract $model_company_abstract, array $values = null)
        {
            parent::__construct();

            $this->model_type_in_company = new Principio_Model_TypeInCompany($model_company_abstract, $model_type);

            if (is_null($values))
            {
                $this->id = -1;
                $this->mac = "";
                $this->serial = "";
                $this->ip = "";
                $this->last_prov = "0000:00:00 00-00-00";
                $this->last_firmware = "";
                $this->prov_status = "";
                $this->last_prov_attempt = "0000:00:00 00-00-00";
                $this->last_prov_msg = "";

                $this->initTemplates($model_type->getExts());
            }
            else
            {
                $this->id = intval($values['id']);
                $this->mac = $values['mac'];
                $this->serial = $values['serial'];
                $this->ip = $values['ip'];
                $this->last_prov = $values['last_prov'];
                $this->last_firmware = $values['last_firmware'];
                $this->prov_status = $values['prov_status'];
                $this->last_prov_attempt = $values['last_prov_attempt'];
                $this->last_prov_msg = $values['last_prov_msg'];

                $this->initTemplates();
            };

            $this->collection_account_link = Principio_Model_AccountLinkMapper::fetch($this);
        }


        public function _getID() { return $this->id; }


        public static function makeCollection(array $values = null, PrincipioLab_IComparator $comparator = null)
        {
            $collection = static::getLinkedList($comparator);

            if (!is_null($values))
            {
                $model_type = Principio_Model_TypeMapper::fetch($values['type_id']);
                $model_company = Principio_Model_Company_AbstractMapper::fetch($values['company_id']);

                foreach ($values as $value)
                    $collection->addItem(new Principio_Model_Spa($model_type, $model_company, $value));
            };

            return $collection;
        }


        public static function makeCollectionFromTypeInCompany(Principio_Model_TypeInCompany $model_type_in_company, array $values = null, PrincipioLab_IComparator $comparator = null)
        {
            $collection = static::getLinkedList($comparator);

            if (!is_null($values))
            {
                foreach ($values as $value)
                    $collection->addItem(new Principio_Model_Spa($model_type_in_company->getModelType(), $model_type_in_company->getModelCompany(), $value));
            };

            return $collection;
        }


        public static function fromArray($values)
        {
            return (new Principio_Model_Spa(
                Principio_Model_TypeMapper::fetch($values['type_id']),
                Principio_Model_Company_AbstractMapper::fetch($values['company_id']),
                $values)
            );
        }


        public function getID() { return $this->id; }
        public function getMAC() { return $this->mac; }
        public function getSerial() { return $this->serial; }
        public function getIP() { return $this->ip; }
        public function getLastProv() { return $this->last_prov; }
        public function getLastFirmware() { return $this->last_firmware; }
        public function getLastProvAttempt() { return $this->last_prov_attempt; }
        public function getProvStatus() { return $this->prov_status; }
        public function getLastProvMsg() { return $this->last_prov_msg; }
        public function getModelTypeInCompany() { return $this->model_type_in_company; }
        public function getModelType() { return $this->getModelTypeInCompany()->getModelType(); }
        public function getModelCompany() { return $this->getModelTypeInCompany()->getModelCompany(); }


        public function setMAC($MAC)
        {
            $this->mac = $MAC;
            return $this;
        }


        public function setSerial($serial)
        {
            $this->serial = $serial;
            return $this;
        }


        public function setIP($IP)
        {
            $this->ip = $IP;
            return $this;
        }


        public function setLastProv($last_prov)
        {
            $this->last_prov = $last_prov;
            return $this;
        }


        public function setLastFirmware($last_firmware)
        {
            $this->last_firmware = $last_firmware;
            return $this;
        }


        public function setProvStatus($prov_status)
        {
            $this->prov_status = $prov_status;
            return $this;
        }


        public function setLastProvAttempt($last_prov_attempt)
        {
            $this->last_prov_attempt = $last_prov_attempt;
            return $this;
        }


        public function setLastProvMsg($last_prov_msg)
        {
            $this->last_prov_msg = $last_prov_msg;
            return $this;
        }


        public function setID($id)
        {
            $this->id = intval($id);
            return $this;
        }


        /*
         * Accounts
         */
        public function setModelAccount($index, Principio_Model_Account $model_account)
        {
            $this->collection_account_link->getItem(intval($index))->setModelAccount($model_account);
            return $this;
        }


        public function getModelAccount($index)
        {
            return $this->collection_account_link->getItem(intval($index))->getModelAccount();
        }


        public function getAccountLinkCollection() { return $this->collection_account_link; }


        protected function fetchTemplates() { return Principio_Model_SpaExtTemplateMapper::fetch($this); }
        public function addTemplates() { return Principio_Model_SpaExtTemplateMapper::insert($this); }
        public function saveTemplates() { return Principio_Model_SpaExtTemplateMapper::update($this); }
        public function deleteTemplates() { return Principio_Model_SpaExtTemplateMapper::delete($this); }


        public function add() { return Principio_Model_SpaMapper::save($this); }
        public function save() { return Principio_Model_SpaMapper::save($this); }
        public function delete($use_transaction = true) { return Principio_Model_SpaMapper::delete($this, $use_transaction); }
    }
?>